Java By Comparison: Become a Java Craftsman in 70 Examples by Simon Harrer & Jörg Lenhard & Linus Dietz
Author:Simon Harrer & Jörg Lenhard & Linus Dietz [Harrer, Simon]
Language: eng
Format: epub
Tags: Pragmatic Bookshelf
Publisher: Pragmatic Bookshelf
Published: 2018-03-22T00:00:00+00:00
Avoid Breaking the Cause Chain
class TransmissionParser {
static Transmission parse(String rawMessage) {
if (rawMessage != null
&& rawMessage.length() != Transmission.MESSAGE_LENGTH) {
throw new IllegalArgumentException(
String.format("Expected %d, but got %d characters in '%s'",
Transmission.MESSAGE_LENGTH, rawMessage.length(),
rawMessage));
}
String rawId = rawMessage.substring(0, Transmission.ID_LENGTH);
String rawContent = rawMessage.substring(Transmission.ID_LENGTH);
try {
int id = Integer.parseInt(rawId);
String content = rawContent.trim();
return new Transmission(id, content);
} catch (NumberFormatException e) {
» throw new IllegalArgumentException(
String.format("Expected number, but got '%s' in '%s'",
rawId, rawMessage));
}
}
}
Exceptions can cause more exceptions. When an exception is caught but cannot be handled, it must be rethrown. If a bug is involved, this can propagate until the program crashes. If the error handling was done right, you will be presented with a stack trace that represents the cause chain—a list where each exception is linked to the one that caused it. When tracking down a bug, a detailed cause chain is worth a pile of gold.
That’s why you need to make sure that you never break this chain. For instance, look at the code above. It catches the NumberFormatException and throws a new IllegalArgumentException with an informative message instead. No bad exception handling as such—but it breaks the cause chain!
Can you spot the problem? It’s the IllegalArgumentException not getting the reference to its cause, the NumberFormatException. Without this link there’s no cause chain. When you look at the stack trace of this IllegalArgumentException, you won’t find a hint that it stems from a NumberFormatException or from what line in the code. We lost a lot of useful information—the NumberFormatException has its own message that provides more information and context on another level of abstraction and its own stack trace with line numbers.
So how can we keep the cause chain instead of breaking it?
class TransmissionParser {
static Transmission parse(String rawMessage) {
if (rawMessage != null
&& rawMessage.length() != Transmission.MESSAGE_LENGTH) {
throw new IllegalArgumentException(
String.format("Expected %d, but got %d characters in '%s'",
Transmission.MESSAGE_LENGTH, rawMessage.length(),
rawMessage));
}
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Hello! Python by Anthony Briggs(9914)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9795)
The Mikado Method by Ola Ellnestam Daniel Brolund(9777)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8296)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7778)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7763)
Grails in Action by Glen Smith Peter Ledbrook(7696)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7557)
Windows APT Warfare by Sheng-Hao Ma(6828)
Layered Design for Ruby on Rails Applications by Vladimir Dementyev(6558)
Blueprints Visual Scripting for Unreal Engine 5 - Third Edition by Marcos Romero & Brenden Sewell(6425)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6413)
Kotlin in Action by Dmitry Jemerov(5062)
Hands-On Full-Stack Web Development with GraphQL and React by Sebastian Grebe(4316)
Functional Programming in JavaScript by Mantyla Dan(4038)
Solidity Programming Essentials by Ritesh Modi(3996)
WordPress Plugin Development Cookbook by Yannick Lefebvre(3788)
Unity 3D Game Development by Anthony Davis & Travis Baptiste & Russell Craig & Ryan Stunkel(3732)
The Ultimate iOS Interview Playbook by Avi Tsadok(3707)
